//
//  FKMyCouponVC.m
//  HappyTest
//
//  Created by Macbook Pro on 2019/1/2.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKMyCouponVC.h"
#import <JXCategoryView.h>
#import "FKCouponListVC.h"

@interface FKMyCouponVC ()<JXCategoryViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (nonatomic, weak) JXCategoryTitleView *categoryView;

@end

@implementation FKMyCouponVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的优惠券";
    // 默认为user类型
    NSArray *titles = @[@"未使用(0)", @"已使用(0)", @"已过期(0)"];
    NSArray *statuses = @[@"1", @"2", @"3"];
    CGFloat headerH = 44;
    JXCategoryTitleView *categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, headerH)];
    [self.topView addSubview:categoryView];
    
    self.categoryView = categoryView;
    categoryView.titleSelectedColor = fkMainColor;
    categoryView.delegate = self;
    categoryView.titleColorGradientEnabled = YES;
    categoryView.titles = titles;
    categoryView.backgroundColor = [UIColor whiteColor];
    
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    categoryView.indicators = @[lineView];
    lineView.indicatorLineViewColor = fkMainColor;
    
    self.scrollView.bounces = false;
    self.scrollView.alwaysBounceHorizontal = false;
    categoryView.contentScrollView = self.scrollView;
    self.scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag | UIScrollViewKeyboardDismissModeInteractive;
    
    for (NSInteger index = 0; index < titles.count; index++) {
        // FKCouponListVC不能继承自UITableViewController，否则会导致宽度无法改变
        FKCouponListVC *vc = [[FKCouponListVC alloc] init];
        [self addChildViewController:vc];
        vc.title = titles[index];
        vc.status = statuses[index];
        fkWeakSelf(self);
        vc.refreshTopTitles = ^(NSDictionary * _Nonnull titleDict) {
            NSMutableArray *array = [NSMutableArray array];
            [array addObject:fkFORMAT(@"未使用(%@)", titleDict[@"notUsed"] ? : @"0")];
            [array addObject:fkFORMAT(@"已使用(%@)", titleDict[@"AlreadyUsed"] ? : @"0")];
            [array addObject:fkFORMAT(@"已过期(%@)", titleDict[@"Expired"] ? : @"0")];
            weakself.categoryView.titles = array;
            [weakself.categoryView reloadData];
        };
        vc.view.frame = CGRectMake(fkScreenW * index, 0, fkScreenW, self.scrollView.fk_height);
        [self.scrollView addSubview:vc.view];
        
        // 让第一个控制器加载数据
        if (self.type) {
            if ([self.type isEqualToString:vc.status]) {
                [vc refreshData];
                categoryView.defaultSelectedIndex = index;
            }
        } else {
            if (index == 0) [vc refreshData];
        }
    }
    self.scrollView.contentSize = CGSizeMake(fkScreenW * titles.count, 0);
}


#pragma mark - JXCategoryViewDelegate
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index
{
    FKCouponListVC *vc = self.childViewControllers[index];
    [vc refreshData];
}


@end
